package org.wxl.controller;

import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import org.wxl.comment.BussinessTypeConstant;
import org.wxl.comment.R;
import org.wxl.domain.fileInfoEntity;
import org.wxl.service.FileUploadService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author 王学良
 */
@RestController
@RequestMapping("/file")
@AllArgsConstructor
public class FileController {

    private final FileUploadService fileUploadService;
    /**
     * 上传视频
     * @param file
     * @param uploaderUuid
     * @return
     */
    @Operation(summary = "上传视频")
    @PostMapping("/upload/video")
    public R uploadMp4(@RequestParam("file") MultipartFile file, @RequestParam("uploaderUuid") String uploaderUuid) {
        try {
            fileInfoEntity fileInfoEntity = fileUploadService.uploadFile(file, uploaderUuid, BussinessTypeConstant.USER_VIDEO);
            return R.ok().message("视频上传成功").data("videoUuid", fileInfoEntity.getUuid());
        } catch (IOException e) {
            return R.error().message("视频上传失败");
        }
    }
    @Operation(summary = "上传视频缩略图")
    @PostMapping("/upload/video/thumbnail")
    public R uploadVideoThumbnail(@RequestParam("file") MultipartFile file, @RequestParam("uploaderUuid") String uploaderUuid, @RequestParam("videoUuid") String videoUuid) {
        try {
            String videoUuid1= fileUploadService.uploadVideoThumbnail(file, uploaderUuid,videoUuid );
            return R.ok().message("视频缩略图上传成功").data("videoThumbnailUuid",videoUuid1);
        } catch (IOException e) {
            return R.error().message("视频缩略图上传失败");
        }
    }
    @Operation(summary = "更新视频信息")
    @PutMapping("/update/video")
    public R updateVideoInfo(@RequestBody fileInfoEntity fileInfoEntity) {
        String updateFileInfo = fileUploadService.updateFileInfo(fileInfoEntity);
        return R.ok().message(updateFileInfo);
    }

}
